﻿using NPOI.POIFS.FileSystem;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Web;

namespace EPlatform_web.App_Start
{
    public class LDAPHelper
    {
        private System.DirectoryServices.DirectoryEntry _objDirectoryEntry;
        /// <summary> 
        /// 构造函数 
        /// </summary> 
        /// <param name="LADPath">ldap的地址</param> 
        /// <param name="authUserName">连接用户名</param> 
        /// <param name="authPWD">连接密码</param> 
        public bool OpenConnection(string LADPath, string authUserName, string authPWD)
        { 
            //创建一个连接 
            _objDirectoryEntry = new System.DirectoryServices.DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None);

            if (null == _objDirectoryEntry)
            {
                return false;
            }
            else if (_objDirectoryEntry.Properties != null && _objDirectoryEntry.Properties.Count > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary> 
        /// 检测一个用户和密码是否正确 
        /// </summary> 
        /// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param> 
        /// <param name="TestUserID">testuserid</param> 
        /// <param name="TestUserPwd">testuserpassword</param> 
        /// <param name="ErrorMessage"></param> 
        /// <returns></returns> 
        public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage)
        {
            bool blRet = false;
            try
            {
                //创建一个检索 
                DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry)
                {
                    //过滤名称是否存在 
                    Filter = strLDAPFilter,
                    SearchScope = SearchScope.Subtree
                };

                //find the first instance 
                SearchResult objSearResult = deSearch.FindOne();

                //如果用户密码为空 
                if (string.IsNullOrEmpty(TestUserPwd))
                {
                    if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > 0)
                    {
                        blRet = true;
                    }
                }
                else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path))
                {
                    //获取用户名路徑对应的用户uid 
                    int pos = objSearResult.Path.LastIndexOf('/');
                    string uid = objSearResult.Path.Remove(0, pos + 1);
                    System.DirectoryServices.DirectoryEntry objUserEntry = new System.DirectoryServices.DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None);
                    if (null != objUserEntry && objUserEntry.Properties.Count > 0)
                    {
                        blRet = true;
                    }
                }
            }
            catch (Exception ex)
            {
                if (null != _objDirectoryEntry)
                {
                    _objDirectoryEntry.Close();
                }
                ErrorMessage = "检测异常：" + ex.Message;
            }
            return blRet;
        }
        /// <summary> 
        /// 关闭连接 
        /// </summary> 
        public void CloseConnection()
        {
            if (null != _objDirectoryEntry)
            {
                _objDirectoryEntry.Close();
            }
        }
    }
}